package com.kevin.j2eeToolBox.io.file;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

public class FileTest4 {

	private static int time = 0;

	public static void main(String[] args) {
		File file = new File("D:\\OsakaCity");
		deepList(file);
	}

	public static void deepList(File file) {

		if (file.isFile() || 0 == file.listFiles().length) {

			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < time; i++) {
				sb.append("\t");
			}

			System.out.println(sb.append(file.getName()).toString());

			return;
		} else {

			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < time; i++) {
				sb.append("\t");
			}
			System.out.println(sb.append(file.getName()).toString());

			File[] fileList = file.listFiles();

			Arrays.sort(fileList, new Comparator<File>() {
				@Override
				public int compare(File o1, File o2) {
					if (o1.isFile() && o2.isDirectory())
						return -1;
					else if (o1.isFile() && o2.isFile() || o1.isDirectory()
							&& o2.isDirectory())
						return o1.getName().compareTo(o2.getName());
					else
						return 1;
				}
			});

			for (File item : fileList) {

				time++;
				deepList(item);
				time--;

			}
		}
	}
}
